﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using SoundEdge.Dashboard.Project;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition;
using System.IO;
using System.Reflection;

namespace SoundEdge.Dashboard
{
    [Export]
    public partial class MainForm : DockContent
    {
        private List<SoundEdgeProject> _projects = new List<SoundEdgeProject>();

        public SoundEdgeProject ActiveProject
        {
            get
            {
                return _projects.FirstOrDefault(item => item._designer != null && item._designer.Active);
            }
        }

        private frmParserLibrary _libForm = null;
        public frmParserLibrary ParserLibraryForm
        {
            get
            {
                if (_libForm == null || _libForm.IsDisposed)
                {
                    _libForm = new frmParserLibrary();
                }
                return _libForm;
            }
        }

        private frmMonitor _monitorForm = null;
        public frmMonitor MonitorForm
        {
            get
            {
                if (_monitorForm == null || _monitorForm.IsDisposed)
                {
                    _monitorForm = new frmMonitor();
                }
                return _monitorForm;
            }
        }

        private frmStatus _statusForm = null;
        public frmStatus StatusForm
        {
            get
            {
                if (_statusForm == null || _monitorForm.IsDisposed)
                {
                    _statusForm = new frmStatus();
                }
                return _statusForm;
            }
        }

        public MainForm()
        {
            InitializeComponent();

            ParserLibraryForm.ShowHint = DockState.Float;
            ParserLibraryForm.Show(dockPanel1, WeifenLuo.WinFormsUI.Docking.DockState.DockLeftAutoHide);

            MonitorForm.ShowHint = DockState.Float;
            MonitorForm.Show(dockPanel1, WeifenLuo.WinFormsUI.Docking.DockState.DockRight);

            StatusForm.ShowHint = DockState.Float;
            StatusForm.Show(dockPanel1, WeifenLuo.WinFormsUI.Docking.DockState.DockBottomAutoHide);
        }

        #region non ui

        public void NewProject()
        {
            Guid id = Guid.NewGuid();
            SoundEdgeProject prj = new SoundEdgeProject();
            frmDesign designForm = new frmDesign(prj) { Text = string.Format("New Project{0}", _projects.Count == 0 ? "" : string.Format(" [{0}]", _projects.Count)) };
            prj.SetDesignerForm(designForm, dockPanel1, false);
            _projects.Add(prj);
            saveToolStripMenuItem.Enabled = _projects.Count > 0;
            saveAsToolStripMenuItem.Enabled = _projects.Count > 0;
            saveToolStripButton.Enabled = _projects.Count > 0;
            saveAllToolStripMenuItem.Enabled = _projects.Count > 0;
            saveAllToolStripButton.Enabled = _projects.Count > 0;
        }

        public void LoadProject()
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                SoundEdgeProject prj = SoundEdgeProject.Load(openFileDialog1.FileName, null, null) as SoundEdgeProject;
                frmDesign designForm = new frmDesign(prj) { Text = string.Format("New Project{0}", _projects.Count == 0 ? "" : string.Format(" [{0}]", _projects.Count)) };
                prj.SetDesignerForm(designForm, dockPanel1, true);

                _projects.Add(prj);
            }
            saveToolStripMenuItem.Enabled = _projects.Count > 0;
            saveAsToolStripMenuItem.Enabled = _projects.Count > 0;
            saveToolStripButton.Enabled = _projects.Count > 0;
            saveAllToolStripMenuItem.Enabled = _projects.Count > 0;
            saveAllToolStripButton.Enabled = _projects.Count > 0;
        }

        public void SaveAllProjects()
        {
            foreach (SoundEdgeProject prj in _projects)
            {
                Utils.SaveProject(prj, prj._fileName);
            }
        }
        #endregion

        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            NewProject();
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            LoadProject();
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewProject();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadProject();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ActiveProject != null)
            {
                Utils.SaveProject(ActiveProject, ActiveProject._fileName);
            }
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            if (ActiveProject != null)
            {
                Utils.SaveProject(ActiveProject, ActiveProject._fileName);
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Utils.SaveProject(ActiveProject, null);
        }

        private void dockPanel1_DoubleClick(object sender, EventArgs e)
        {
            LoadProject();
        }

        private void saveAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveAllProjects();
        }

        private void saveAllToolStripButton_Click(object sender, EventArgs e)
        {
            SaveAllProjects();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ////An aggregate catalog that combines multiple catalogs
            //var catalog = new AggregateCatalog();
            ////Adds all the parts found in all assemblies in 
            ////the same directory as the executing program
            //catalog.Catalogs.Add(new DirectoryCatalog(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)));
            //catalog.Catalogs.Add(new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()));
            
            ////Create the CompositionContainer with the parts in the catalog
            //CompositionContainer container = new CompositionContainer(catalog);

            ////Fill the imports of this object
            //container.ComposeParts(this);

            //frmSettings settings = container.GetExportedValue<frmSettings>();
            frmSettings settings = new frmSettings();
            settings.ShowDialog(this);
        }
    }
}
